package com.taikang.hmp.hbusiness.diabetes.measure.sugar.action.intface;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.ErrorConstant;
import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.frame.common.web.session.AppSessionFactory;
import com.taikang.hmp.frame.common.web.session.SessionBean;
import com.taikang.hmp.hbusiness.diabetes.measure.job.service.ISugarMonitorAutoJobService;
import com.taikang.hmp.hbusiness.diabetes.measure.sugar.service.ISugarService;


/** 
 * 血糖
 * author:qrp
 * time:2015年7月24日 上午10:02:38
 */
public class SugarAction extends BaseAction{
	private static Logger logger = LoggerFactory.getLogger(SugarAction.class);
	
	
	private ISugarService sugarService;
	private ISugarMonitorAutoJobService sugarMonitorAutoJobService;

	
	public ISugarService getSugarService() {
		return sugarService;
	}

	public void setSugarService(ISugarService sugarService) {
		this.sugarService = sugarService;
	}

	public ISugarMonitorAutoJobService getSugarMonitorAutoJobService() {
		return sugarMonitorAutoJobService;
	}

	public void setSugarMonitorAutoJobService(
			ISugarMonitorAutoJobService sugarMonitorAutoJobService) {
		this.sugarMonitorAutoJobService = sugarMonitorAutoJobService;
	}

	public String executeAction() {
		if ("saveSugarPlan".equals(requestDto.getMethod())) {
			return saveSugarPlan();
		}else if("getSugarPlan".equals(requestDto.getMethod())){
			return getSugarPlan();
		}else if("saveSugar".equals(requestDto.getMethod())){
			logger.info("--保存血糖测量值 saveSugar method--");
			return saveSugar();
		}else if("delSugar".equals(requestDto.getMethod())){
			logger.info("--删除血糖测量值 delSugar method--");
			return saveSugar();
		}else if("getSugarList".equals(requestDto.getMethod())){
			logger.info("--获取血糖测量值列表 getSugarList method--");
			return getSugarList();
		}else if("getPlanSugarCount".equals(requestDto.getMethod())){
			logger.info("--按计划测量血糖次数--getPlanSugarCount method--");
			return getPlanSugarCount();
		}else if("saveSugar4Doctor".equals(requestDto.getMethod())){
			logger.info("--保存血糖测量值【泰康医生】--saveSugar4Doctor method--");
			return saveSugar4Doctor();
		}else if("delSugar4Doctor".equals(requestDto.getMethod())){
			logger.info("--删除血糖测量值【泰康医生】--delSugar4Doctor method--");
			return delSugar4Doctor();
		}else if("getSugarList4Doctor".equals(requestDto.getMethod())){
			logger.info("--获取血糖测量值【泰康医生】--getSugarList4Doctor method--");
			return getSugarList4Doctor();
		}else {
			throw new TKException("no support method:"+requestDto.getMethod());
		}
	}
	
	/**
	 * 保存血糖测量计划
	 */
	public String saveSugarPlan(){	
		ResponseDto responseDto = new ResponseDto();
		
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		//
		sugarService.saveSugarPlan(paramDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	
	
	/**
	 * 获取血糖测量计划
	 */
	public String getSugarPlan(){
		ResponseDto responseDto = new ResponseDto();
		
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		String customerid = paramDto.getAsString("customerid");
        //
		String sessionId = requestDto.getSessionid();
		SessionBean sessionBean = AppSessionFactory.getInstance(requestDto.getRequestsource()).isSessionTimeout(sessionId);
		if(sessionBean != null){
			//检查并初始化血糖监测计划
			boolean flag = sugarMonitorAutoJobService.isNotInitPlan(customerid);
			if(flag){
				sugarMonitorAutoJobService.saveInitSugarPlan(customerid);
				sugarMonitorAutoJobService.updateInitPlanStatus(customerid,"1");
			}
			List<Dto> planList = sugarService.getSugarPlan(paramDto);
			Dto resultDto = new BaseDto();
			resultDto.put("planlist", planList);
			responseDto.pushResultDto(resultDto);
		}else{
			responseDto.addErrorCode(ErrorConstant.SESSION_TIMEOUT);
		}
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	/**
	 * 保存血糖测量值
	 * @return
	 */
	public String saveSugar() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		//
		String result = sugarService.saveSugar(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("result", result);
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 删除血糖测量值
	 * @return
	 */
	public String delSugar(){
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		Dto resultDto = new BaseDto();
		String result = sugarService.delSugar(paramDto);
		resultDto.put("result", result);
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 获取血糖测量值列表
	 */
	public String getSugarList(){		
		ResponseDto responseDto = new ResponseDto();
		//获取参数
        Dto paramDto = requestDto.getRequestlist().get(0);
        paramDto = super.appendRequestBase(paramDto, requestDto);     
        //
        List<Object> resultList = sugarService.getSugarList(paramDto);
        Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	/**
	 * 按计划测量血糖次数
	 */
	public String getPlanSugarCount(){	
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		//
		int resultCount = sugarService.getPlanSugarCount(paramDto);	
		Dto resultDto = new BaseDto();
		resultDto.put("resultcount", resultCount+"");
		responseDto.pushResultDto(resultDto);
		//
	    super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	/**
	 * 保存血糖测量值【泰康医生】
	 * @return
	 */
	public String saveSugar4Doctor() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		//判别身份，标志位
		String state = sugarService.getUserType(requestDto.getUid());
		Dto resultDto = new BaseDto();
		if(!state.equals("0")){
			String result = sugarService.saveSugar(paramDto);
			resultDto.put("result", result);
			resultDto.put("resultcode", "success");
		}else{
			resultDto.put("result", "");
			resultDto.put("resultcode", "error");//泰康医生正常用户不允许操作
		}
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 删除血糖测量值【泰康医生】
	 * @return
	 */
	public String delSugar4Doctor(){
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		//判别身份，标志位
		String state = sugarService.getUserType(requestDto.getUid());
		Dto resultDto = new BaseDto();
		if(!state.equals("0")){
			String result = sugarService.delSugar(paramDto);
			resultDto.put("result", result);
			resultDto.put("resultcode", "success");
		}else{
			resultDto.put("result", "");
			resultDto.put("resultcode", "error");//泰康医生正常用户不允许操作
		}
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	/**
	 * 获取血糖测量值列表【泰康医生】
	 */
	public String getSugarList4Doctor(){		
		ResponseDto responseDto = new ResponseDto();
		//获取参数
        Dto paramDto = requestDto.getRequestlist().get(0);
        paramDto = super.appendRequestBase(paramDto, requestDto);     
        //
        List<Object> resultList = sugarService.getSugarListDoctor(paramDto);
        Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
}
